/* Copyright (c) 2006 by M Aamir Maniar 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be 
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * */

namespace Generic.DataStructures
{
	using System;
	using System.Collections.Generic;
	using System.Text;

	public class SinglyLinkedListNode<T> : IDisposable
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="SinglyLinkedListNode&lt;T&gt;"/> class.
		/// </summary>
		public SinglyLinkedListNode(T value)
		{
			_value = value;
		}

		private T _value;
		/// <summary>
		/// Gets or sets the value.
		/// </summary>
		/// <value>The value.</value>
		public T Value
		{
			get { return _value; }
			set { _value = value; }
		}

		private SinglyLinkedListNode<T> _next;
		/// <summary>
		/// Gets or sets the next node.
		/// </summary>
		/// <value>The next node.</value>
		public virtual SinglyLinkedListNode<T> Next
		{
			get { return _next; }
		}

		protected void SetNext(SinglyLinkedListNode<T> node)
		{
			_next = node;
		}

		private SinglyLinkedList<T> _list;
		public virtual SinglyLinkedList<T> List
		{
			get { return _list; }
		}

		protected void SetList(SinglyLinkedList<T> list)
		{
			_list = list;
		}

		public bool IsHead
		{
			get
			{
				if (_list == null)
					return false;
				return _list.Head.Equals(this);
			}
		}

		public bool IsTail
		{
			get
			{
				if (_list == null)
					return false;
				return _list.Tail.Equals(this);
			}
		}

		#region IDisposable Members

		/// <summary>
		/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
		/// </summary>
		public void Dispose()
		{
			if (_value is IDisposable)
				((IDisposable)_value).Dispose();
			_next = null;
			_value = default(T);
			GC.SuppressFinalize(this);
		}

		#endregion

	}
}
